<?php

namespace app\forms;

use app\models\Group;
use app\models\Position;
use app\models\Department;
use app\components\validators\Exist;
use app\components\helpers\MappingHelper;
use app\components\validators\PositiveInteger;
use yii\db\Query;

/**
 * 岗位表单类
 */
class PositionForm extends Model
{
    public $id;
    public $groupId;
    public $deptId;
    public $parentId;
    public $name;
    public $setDefault = 0;
    public $childId = 0;

    /**
     * @var array 要赋予的权限项
     */
    public $item;

    /**
     * @var array 赋予的权限项对应的授权数据类型
     */
    public $auth;

    /**
     * @var array 赋予的权限项对应的自定义授权数据
     */
    public $data;

    /**
     * @var integer 权限项ID
     */
    public $itemId;

    /**
     * @var array 权限赋值记录 position_item 表的一条记录
     */
    public $itemData;

    public function rules()
    {
        return [
            ['id', PositiveInteger::class],

            ['groupId', PositiveInteger::class],
            ['groupId', 'exist', 'targetClass' => Group::class, 'targetAttribute' => 'id'],

            ['deptId', PositiveInteger::class],
            ['deptId', 'exist', 'targetClass' => Department::class, 'targetAttribute' => 'id'],

            ['parentId', 'integer'],
            ['parentId', 'default', 'value' => 0],
            ['parentId', Exist::class, 'targetClass' => Position::class, 'targetAttribute' => 'id'],

            ['name', 'trim'],
            ['name', 'string', 'max' => 32],

            ['setDefault', 'boolean'],

            ['item', 'each', 'rule' => ['integer']],

            ['auth', 'default', 'value' => ''],

            ['data', 'default', 'value' => ''],

            ['childId', 'integer'],
            ['childId', Exist::class, 'targetClass' => Position::class, 'targetAttribute' => 'id'],

            ['id', 'required', 'on' => ['update', 'delete', 'view', 'get-info', 'get-item-data', 'get-default-item']],
            [
                'id',
                'exist',
                'targetClass' => Position::class,
                'targetAttribute' => 'id',
                'on' => ['update', 'delete', 'view', 'get-info', 'get-default-item']
            ],

            ['itemId', PositiveInteger::class, 'on' => ['get-item-data']],
            ['itemId', 'required', 'on' => ['get-item-data']],
            ['itemId', 'checkItemData'],

            ['groupId', 'required', 'on' => ['create']],

            ['deptId', 'required', 'on' => ['create']],

            ['name', 'required', 'on' => ['create', 'update']],

            [
                'groupId',
                'required',
                'when' => function ($model) {
                    return empty($model->deptId);
                },
                'message' => 'groupId or deptId is required',
                'on' => ['get-tree-list']
            ],
            [
                'deptId',
                'required',
                'when' => function ($model) {
                    return empty($model->groupId);
                },
                'message' => 'groupId or deptId is required',
                'on' => ['get-tree-list']
            ],
            [
                'groupId',
                Exist::class,
                'targetClass' => Group::class,
                'targetAttribute' => 'id',
                'on' => ['get-tree-list']
            ],
            [
                'deptId',
                Exist::class,
                'targetClass' => Department::class,
                'targetAttribute' => 'id',
                'on' => ['get-tree-list']
            ],
        ];
    }

    public function checkItemData($attribute, $param)
    {
        $query = new Query();
        $query->from('{{%position_item}}');
        $query->where(['position_id' => $this->id, 'item_id' => $this->$attribute]);
        $itemData = $query->one();
        if (!empty($itemData) && $itemData['data']) {
            $this->itemData = $itemData['data'];
        } else {
            $this->itemData = [];
        }
    }

    /**
     * @return array|array[]|\string[][]
     */
    public function scenarios()
    {
        return [
            'create' => ['groupId', 'deptId', 'parentId', 'name', 'item', 'auth', 'data', 'setDefault', 'childId'],
            'update' => ['id', 'parentId', 'name', 'item', 'auth', 'data', 'setDefault'],
            'get-drop-list' => ['groupId', 'deptId'],
            'get-tree-list' => ['groupId', 'deptId'],
            'get-item-data' => ['id', 'itemId'],
            'get-default-item' => ['id'],
            'get-info' => ['id'],
            'delete' => ['id'],
            'view' => ['id'],
        ];
    }
}